<?php

class ShipSchedule extends Wp_ship_schedule_model {

    function __construct() {
        parent::__construct();
    }
    
    function getList($filter = array()) {
        $ship_schedule = new ShipSchedule();
        if(isset($filter['name']) && $filter['name']) {
            $ship_schedule->addWhere("wp_ship_schedule.ship_name LIKE '%".$filter['name']."%'");
        }
        if(isset($filter['country']) && $filter['country']) {
            $ship_schedule->addWhere("wp_ship_schedule.id_wp_country = ".intval($filter['country']));
        }
        if(isset($filter['type']) && $filter['type']) {
            $ship_schedule->addWhere("wp_ship_schedule.type = ".$filter['type']);
            
            if($filter['type'] == 1) {
                $ship_schedule->addJoin(new Port(), "INNER", "wp_port", "wp_port.id = wp_ship_schedule.arriving_port");
            } else if ($filter['type'] == 2) {
                $ship_schedule->addJoin(new Port(), "INNER", "wp_port", "wp_port.id = wp_ship_schedule.leaving_port");
            }
            if(isset($filter['port']) && $filter['port']) {
                $ship_schedule->addWhere("wp_port.name LIKE '%".$filter['port']."%'");
            }
        }
        if(isset($filter['start_date']) && $filter['start_date']) {
            $ship_schedule->addWhere("wp_ship_schedule.date_time >= '".date_to_date_sql($filter['start_date'])."'");
        }
        if(isset($filter['end_date']) && $filter['end_date']) {
            $ship_schedule->addWhere("wp_ship_schedule.date_time <= '".date_to_date_sql($filter['end_date'])."'");
        }
        if(isset($filter['region']) && $filter['region']) {
            $port->addWhere("wp_ship_schedule.id_wp_region = ".intval($filter['region']));
        }
        $ship_schedule->addJoin(new Country());
        $ship_schedule->addJoin(new Region(), 'LEFT');
        $ship_schedule->addSelect();
        $ship_schedule->addSelect("wp_ship_schedule.*, wp_country.name as country_name, wp_port.name as name_port, wp_region.name as region_name");
        $ship_schedule->orderBy("wp_ship_schedule.date_time DESC, wp_ship_schedule.ship_name");
        $ship_schedule->find();
        
        return $ship_schedule;            
    }
    
    function validateInput() {

        if (empty ($this->ship_name)) {
            MessageHandler::add (_MSG_SHIP_NAME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (strlen($this->ship_name) > MAX_LENGTH_NAME) {
                MessageHandler::add (_MSG_SHIP_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
            }
            
            if (self::isExistsByName($this->id, $this->ship_name, $this->date_time)) {
                MessageHandler::add (_MSG_SHIP_NAME_EXISTS, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (!$this->id_wp_country) {
            MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $country = new Country();
            if (!$country->get($this->id_wp_country)) {
                 MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty($this->id_wp_region)) {
            MessageHandler::add (_MSG_REGION_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $region = new Region();
            if (!$region->get($this->id_wp_region)) {
                 MessageHandler::add (_MSG_REGION_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty($this->grt)) {
            MessageHandler::add (_MSG_GRT_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (!is_numeric($this->grt)) {
                MessageHandler::add (_MSG_GRT_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty($this->dwt)) {
            MessageHandler::add (_MSG_DWT_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (!is_numeric($this->dwt)) {
                MessageHandler::add (_MSG_DWT_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty ($this->date_time)) {
            MessageHandler::add (_MSG_DATE_TIME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if(!isValidDate($this->date_time)) {
                MessageHandler::add (_MSG_DATE_TIME_INVALID, MSG_ERROR, LOG_ONLY);
            }
            $this->date_time = date_to_date_sql($this->date_time);
        }
        
        if (empty ($this->arriving_port)) {
            MessageHandler::add (_MSG_ARRIVING_PORT_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $port = new Port();
            if (!$port->get($this->arriving_port)) {
                MessageHandler::add (_MSG_ARRIVING_PORT_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        if (empty ($this->leaving_port)) {
            MessageHandler::add (_MSG_LEAVING_PORT_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $port = new Port();
            if (!$port->get($this->leaving_port)) {
                MessageHandler::add (_MSG_LEAVING_PORT_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if ($this->arriving_port == $this->leaving_port) {
            MessageHandler::add (_MSG_ARRVING_LEAVING_PORT_INVALID, MSG_ERROR, LOG_ONLY);
        }
        
        if (empty($this->draf)) {
            MessageHandler::add (_MSG_DRAF_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (!is_numeric($this->draf)) {
                MessageHandler::add (_MSG_DRAF_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty($this->loa)) {
            MessageHandler::add (_MSG_LOA_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (!is_numeric($this->loa)) {
                MessageHandler::add (_MSG_LOA_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        return MessageHandler::countError() > 0 ? false : true;

    }
    
    public function isExistsByName($id = 0, $name = '', $date_time = '')
	{
		if ($name == '' || $date_time == '')
		{
			return false;
		}	
		$ship_schedule = new ShipSchedule();
		
        if ($id) {
			$ship_schedule->addWhere("id <> {$id}");
		}
                
        $ship_schedule->addWhere("ship_name = '$name'");
		$ship_schedule->addWhere("date_time = '$date_time'");
		$ship_schedule->find();
		return $ship_schedule->countRows() > 0;
	}
        
    public function insert() {
        $this->created_date = date('Y-m-d H:i:s');
        parent::insert();
    }
    
    public function update() {
        $this->modified_date = date('Y-m-d H:i:s');
        parent::update();
    }
}
